package com.lxj.passkeep.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lxj.passkeep.common.enums.BasicEnum;
import com.lxj.passkeep.common.exception.BaseException;
import com.lxj.passkeep.dto.FormDataDto;
import com.lxj.passkeep.enity.FormData;
import com.lxj.passkeep.mapper.FormDataMapper;
import com.lxj.passkeep.service.FormDataService;
import com.lxj.passkeep.vo.FormDataVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FormDataServiceImpl implements FormDataService {

    @Resource
    private FormDataMapper formDataMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<FormDataVo> selectAll() {
        List<FormData> resultList=formDataMapper.selectAll();
        return BeanUtil.copyToList(resultList, FormDataVo.class);
    }

    /**
     * 插入
     * @param formDataDto
     */
    @Override
    public void insert(FormDataDto formDataDto) {
        if (ObjectUtil.isNull(formDataDto)|| ObjectUtil.isEmpty(formDataDto)){
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        FormData formData = BeanUtil.copyProperties(formDataDto, FormData.class);
        formDataMapper.insert(formData);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public FormDataVo getDataById(Integer id) {
        if (ObjectUtil.isNull(id)|| ObjectUtil.isEmpty(id)){
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        FormData formData = formDataMapper.selectById(id);
        if (ObjectUtil.isNotNull(formData)){
            return BeanUtil.copyProperties(formData, FormDataVo.class);
        }
        else {
            throw new BaseException(BasicEnum.DATA_NOT_EXIST);
        }
    }
}
